package Yudisium.view;

import Yudisium.view.Mahasiswa.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import Yudisium.model.*;
//import com.util.FormUtil;
import java.sql.Connection;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class LoginMahasiswa extends JDialog {

    private JLabel mahasiswa, nim;
    private JTextField nimform;
    private JButton login;

    public LoginMahasiswa() {

        this.setTitle("Login Mahasiswa");
        this.setSize(200, 140);
//        FormUtil.centerWindow(this);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setLayout(null);
        this.setResizable(false);
        this.setVisible(true);
        this.setLayout(null);
        this.setVisible(true);

        mahasiswa = new JLabel("Mahasiswa");
        mahasiswa.setBounds(50, 0, 100, 20);
        this.add(mahasiswa);

        nim = new JLabel("NIM");
        nim.setBounds(20, 30, 50, 30);
        this.add(nim);

        nimform = new JTextField();
        nimform.setBounds(75, 30, 100, 30);
        this.add(nimform);

        login = new JButton("Login");
        login.setBounds(50, 70, 100, 30);
        login.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                actionLoginMhs();
                if (e.getSource() == login) {
                    dispose();
                }
            }

            private void actionLoginMhs() {
                String nim = nimform.getText();
                Mahasiswa mhs = new Mahasiswa();
                try {
                    mhs.setNim(nim);
                    mhs.setArrayMahasiswa(nim);
                    Connection con = Conection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");
                    Statement stat = con.createStatement();
                    String sql = "insert into CACHE(DATA)values('" + mhs.getArrayMahasiswa() + "')";
                    stat.executeUpdate(sql);
                    FrameMahasiswa frame = new FrameMahasiswa();
                } catch (Exception ex) {
                    Logger.getLogger(LoginMahasiswa.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "NIM yang Anda masukan salah");
                }
            }
        });
        this.add(login);
    }
}
